﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using Avalonia.Rendering;
using CommunityToolkit.Mvvm.Input;
using Klear.Library.Models;
using Klear.Library.Services;

namespace Klear.Library.ViewModels;

public class IncomeExpensesViewModel : ViewModelBase
{
    private readonly IIncomeAndExpensesService _incomeAndExpensesService;
    public IncomeExpensesViewModel(
        IIncomeAndExpensesService incomeAndExpensesService
    )
    {
        _incomeAndExpensesService = incomeAndExpensesService;
        LoadIncomeExpensesCommand = new AsyncRelayCommand(LoadIncomeExpensesAsync);
    }
    
    // 删除记录部分
    private IncomeAndExpensesModel _selectedIncomeExpense;
    public IncomeAndExpensesModel SelectedIncomeExpense
    {
        get => _selectedIncomeExpense;
        set=>SetProperty(ref _selectedIncomeExpense, value);
    }
    public void DeleteIncomeExpense()
    {
        if (SelectedIncomeExpense == null)
        {
            return;
        }
        _incomeAndExpensesService.Delete(SelectedIncomeExpense);
        IncomeExpensesListThisMonth.Remove(SelectedIncomeExpense);
        SelectedIncomeExpense = null; 
    }
    
    //加载本月收支数据
    public ICommand LoadIncomeExpensesCommand { get; }
    private ObservableCollection<IncomeAndExpensesModel> _incomeExpensesListThisMonth = new();
    public ObservableCollection<IncomeAndExpensesModel> IncomeExpensesListThisMonth
    {
        get => _incomeExpensesListThisMonth;
        set => SetProperty(ref _incomeExpensesListThisMonth, value);
    }
    private ObservableCollection<string> _categoryList = new();
    public ObservableCollection<string> CategoriesList
    {
        get => _categoryList;
        set => SetProperty(ref _categoryList, value);
    }
    public async Task LoadCategoryListAsync()
    {
        var categories = await _incomeAndExpensesService.LoadIncomeAsync();
        foreach (var item in categories)
        {
            CategoriesList.Add(item.CategoryName);
        }
    }
    
    //每日简讯部分
    private DailyInfoModel _daily = new();
    public DailyInfoModel ApiData {
        get => _daily;
        set => SetProperty(ref _daily, value); // ViewModelBase 中实现的通知更新
    }
    //加载
    public async Task LoadIncomeExpensesAsync()
    {
        InterfaceService interfaceService = new InterfaceService();
        DailyInfoModel dailyInfoAsync =await interfaceService.GetDailyInfoAsync();
        IncomeExpensesListThisMonth.Clear();
        if (dailyInfoAsync != null)
        {
            ApiData = dailyInfoAsync;
            OnPropertyChanged(nameof(ApiData));
        }
        var res=await _incomeAndExpensesService.LoadIncomeAndExpensesAsync();
        foreach (var item in res)
        {
            IncomeExpensesListThisMonth.Add(item);
        }
    }
    
    //增加记录部分
    private AddRecordModel _addRecordModel = new();
    public AddRecordModel AddRecord
    {
        get => _addRecordModel;
        set => SetProperty(ref _addRecordModel, value);
    }
    private void AddIncomeExpenseAsync(IncomeAndExpensesModel data)
    {
        _incomeAndExpensesService.SaveIncomeAndExpenses(data);
    }
    public void AddIncomeExpense()
    {
        IncomeAndExpensesModel model = new();
        if (AddRecord.Category is null || AddRecord.Amount is Decimal.Zero || AddRecord.Type is "识别失败")
        {
            return;
        }
        model.Title = ClassType;
        model.Category = AddRecord.Category;
        model.Amount= AddRecord.Amount;
        model.Notes= AddRecord.Notes;
        model.CreateTime = DateTime.Now;
        model.UpdateTime = DateTime.Now;
        IncomeExpensesListThisMonth.Add(model);
        AddIncomeExpenseAsync(model);
        ClearAddRecord();
    }
    // OCR + 文字提取 + 模型赋值
    private OcrService _ocrService = new OcrService();
    public async void UploadIncomeExpenseByAiAsync(string url)
    {
        IsLoading = "正在进行OCR处理...";
        string ocr = await _ocrService.GetText(url);
        IsLoading = "OCR处理完成,JSON转换处理完成!";
    
        var information = new FormatInformation();
        IsLoading = "正在处理文本信息...";
        IncomeAndExpensesModel info = await information.SendPrompt(ocr);
    
        IsLoading = "处理完成,正在提取值...";
        
        await Task.Delay(1000); 
        
        AddRecord.Category = info.Category;
        AddRecord.Type = info.Title;
        AddRecord.Amount = info.Amount;
        AddRecord.Notes = info.Notes;
    
        IsLoading = "";
    }

    //清空数据模型
    public void ClearAddRecord()
    {
        AddRecord.Category = "";
        AddRecord.Type = "";
        AddRecord.Amount = 0;
        AddRecord.Notes = "";
    }
    //类别加载
    private string _type;
    public string ClassType
    {
        get => _type;
        set => SetProperty(ref _type, value);
    }
    //弹窗控制
    private string _selectTag;
    public string SelectTag
    {
        get => _selectTag;
        set
        {
            if (SetProperty(ref _selectTag, value))
            {
                UpdateListsBasedOnSelectTag(value);
            }
        }
    }
    private async void UpdateListsBasedOnSelectTag(string tag)
    {
        if (tag == "0")
        {
            ClassType = "收入";
            CategoriesList.Clear();
            ClearAddRecord();
            var categories = await _incomeAndExpensesService.LoadIncomeAsync();
            foreach (var item in categories)
            {
                CategoriesList.Add(item.CategoryName);
            }
        }
        else if (tag == "1")
        {
            ClassType = "支出";
            CategoriesList.Clear();
            ClearAddRecord();
            var categories = await _incomeAndExpensesService.LoadExpensesAsync();
            foreach (var item in categories)
            {
                CategoriesList.Add(item.CategoryName);
            }
        }
    }
    // 图片处理状态
    private string _isLoading;
    public string IsLoading
    {
        get => _isLoading;
        set => SetProperty(ref _isLoading, value);
    }

}

